package com.nondo.dean.community.service;

import com.nondo.dean.common.domain.BaseEntity;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.community.domain.Category;
import com.nondo.dean.community.domain.CategoryExample;
import com.nondo.dean.community.mapper.CategoryMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/4/24
 */
@Component
public class CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    /**
     * 添加
     *
     * @param category
     * @return
     */
    public int add(Category category) {
        return this.categoryMapper.insertSelective(category);
    }

    /**
     * 更新
     *
     * @param category
     * @return
     */
    @Transactional
    public int update(Category category) {
        return this.categoryMapper.updateByPrimaryKeySelective(category);
    }

    /**
     * 删除
     *
     * @param category
     * @return
     */
    public int delete(Category category) {
        category.setDelFlag(Category.DEL_FLAG_DELETE);
        return this.categoryMapper.updateByPrimaryKeySelective(category);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    public Category select(Integer id) {
        return this.categoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据名字查询
     *
     * @param name
     * @return
     */
    public Category selectByName(String name) {
        CategoryExample caregoryExample = new CategoryExample();
        caregoryExample.createCriteria().andNameEqualTo(name).andDelFlagEqualTo(BaseEntity.DEL_FLAG_NORMAL);

        List<Category> brandList = this.categoryMapper.selectByExample(caregoryExample);

        return CollectionUtils.isNotEmpty(brandList) ? brandList.get(0) : null;
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param category
     * @return
     */
    public List<Category> getList(int offset, int limit, Category category) {
        CategoryExample example = new CategoryExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        CategoryExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, category);
        return categoryMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 获取不分页的分类列表
     *
     * @param category
     * @return
     */
    public List<Category> getList(Category category) {
        CategoryExample example = new CategoryExample();
        example.setOrderByClause("create_date DESC");
        CategoryExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, category);
        return categoryMapper.selectByExample(example);
    }

    /**
     * 数据统计
     *
     * @param category
     * @return
     */
    public Long count(Category category) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, category);
        return categoryMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param category
     */
    void setCriteria(CategoryExample.Criteria criteria, Category category) {
        if (category != null) {
            if (category.getId() != null) {
                criteria.andIdEqualTo(category.getId());
            }
            if (StringUtils.isNoneBlank(category.getName())) {
                criteria.andNameEqualTo(category.getName());
            }
        }
        criteria.andDelFlagEqualTo(Category.DEL_FLAG_NORMAL);
    }
}
